package com.zhouk.httpClient;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Create By Zhouke on 2017/08/25
 */
public class SSLClient{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    //post请求方法
    public  String sendPost(String url, String data) {
        String response = null;
        logger.info("url: " + url);
        logger.info("request: " + data);
        try {
            CloseableHttpClient httpClient = null;
            CloseableHttpResponse httpResponse = null;
            try {
                httpClient = HttpClients.createDefault();
                HttpPost httppost = new HttpPost(url);
                StringEntity stringentity = new StringEntity(data,
                        ContentType.create("text/json", "UTF-8"));
                httppost.setEntity(stringentity);
                httpResponse = httpClient.execute(httppost);
                HttpEntity entity = httpResponse.getEntity();
                response = EntityUtils
                        .toString(httpResponse.getEntity());

                logger.info("{header:}", httpResponse.getAllHeaders());
                logger.info("Response Status:{}", httpResponse.getStatusLine());

                return response;

            } finally {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (httpClient != null) {
                    httpClient.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
    //post请求方法
    public  String httpPost(String url, String data) {
        String response = null;
        logger.info("url: " + url);
        logger.info("request: " + data);
        try {
            CloseableHttpClient httpclient = null;
            CloseableHttpResponse httpresponse = null;
            try {
                httpclient = HttpClients.createDefault();
                HttpPost httppost = new HttpPost(url);
                StringEntity stringentity = new StringEntity(data,
                        ContentType.create("text/json", "UTF-8"));
                httppost.setEntity(stringentity);
                httpresponse = httpclient.execute(httppost);
                logger.info("status:{}", httpresponse.getStatusLine().getStatusCode());
                response = EntityUtils
                        .toString(httpresponse.getEntity());

                logger.info("response: " + response);
            } finally {
                if (httpclient != null) {
                    httpclient.close();
                }
                if (httpresponse != null) {
                    httpresponse.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }
}
